﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using DefaultScope;
using Microsoft.Synchronization.ClientServices.IsolatedStorage;
using System.Collections.ObjectModel;

namespace Klient
{
    public partial class MainPage : PhoneApplicationPage
    {
        #region Dependency Property
        public static DependencyProperty AllEmployeesProperty = DependencyProperty.Register(
            "AllEmployees", typeof(ObservableCollection<Employees>), typeof(MainPage), null);
        public ObservableCollection<Employees> AllEmployees
        {
            get
            {
                return (ObservableCollection<Employees>)GetValue(AllEmployeesProperty);
            }
            set
            {
                SetValue(AllEmployeesProperty, value);
            }
        }
        #endregion
        DefaultScopeOfflineContext context;
        
        // Constructor
        public MainPage()
        {
            DataContext = this;

            context = new DefaultScopeOfflineContext("login", new Uri("http://localhost:4000/DefaultScopeSyncService.svc/"));
            
            context.CacheController.ControllerBehavior.SerializationFormat = Microsoft.Synchronization.ClientServices.SerializationFormat.ODataAtom;
            context.CacheController.RefreshCompleted += new EventHandler<Microsoft.Synchronization.ClientServices.RefreshCompletedEventArgs>(CacheController_RefreshCompleted);
            context.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(context_LoadCompleted);
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        
            InitializeComponent();
        }

        void CacheController_RefreshCompleted(object sender, Microsoft.Synchronization.ClientServices.RefreshCompletedEventArgs e)
        {
            if (e.Error != null)
                Dispatcher.BeginInvoke
                    (delegate 
                    {
                        MessageBox.Show(e.Error.ToString());
                    }
                    );
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            context.LoadAsync();
        }

        void context_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            Dispatcher.BeginInvoke(delegate
            {
                AllEmployees = new ObservableCollection<Employees>(context.EmployeesCollection);
                //context.CacheController.RefreshAsync();
                //TODO: Na razie jest zmiana w ToString
                                                                //potem: zrobienie bindingów dla
                                                                //poszczególnych formatek
            });
        }

        private void loginBtn_Click(object sender, RoutedEventArgs e)
        {       
            Employees emp = (Employees)lista.SelectedItem;
            NavigationService.Navigate(new Uri("/Forms/EmployeePage.xaml?employeeID=" + emp.EmployeeID + "&employeeName=" + emp.LastName, UriKind.Relative));
        }

        private void refreshBtn_Click(object sender, RoutedEventArgs e)
        {
            context.CacheController.RefreshAsync();
        }
    }
}